package com.example.service;


import com.example.entity.PatientVisit;
import com.example.entity.Prescription;
import com.example.entity.PrescriptionItem;
import com.example.mapper.PatientVisitMapper;
import com.example.mapper.PrescriptionItemMapper;
import com.example.mapper.PrescriptionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 药单表业务处理
 **/
@Service
public class PrescriptionService {

    @Resource
    private PrescriptionMapper prescriptionMapper;
    @Resource
    private PrescriptionItemMapper prescriptionItemMapper;
    @Resource
    private PatientVisitMapper patientVisitMapper;

    /**
     * 新增
     */
    public void add(Prescription prescription) {
        prescriptionMapper.insert(prescription);
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteById(Integer id) {//药单ID
        List<PrescriptionItem> prescriptionItems = prescriptionItemMapper.selectByPrescriptionId(id);
        //删除该药单中的药品项目
        for (PrescriptionItem item : prescriptionItems) {
            prescriptionItemMapper.deleteById(item.getId());
        }
        //把绑定在该药单的就诊单中的是否开药改成否，并且删除药单ID信息
        PatientVisit patientVisit =patientVisitMapper.selectByPrescriptionId(id);
        patientVisit.setPrescribedMedicine("否");
        patientVisitMapper.updateById(patientVisit);
        patientVisitMapper.updatePrescriptionIdIsNull(patientVisit.getId());

        prescriptionMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    @Transactional
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Prescription prescription) {
        prescriptionMapper.updateById(prescription);
    }

    /**
     * 根据ID查询
     */
    public Prescription selectById(Integer id) {
        return prescriptionMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Prescription> selectAll(Prescription prescription) {
        return prescriptionMapper.selectAll(prescription);
    }

    /**
     * 分页查询
     */
    public PageInfo<Prescription> selectPage(Prescription prescription, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Prescription> list = prescriptionMapper.selectAll(prescription);
        return PageInfo.of(list);
    }


    public Prescription addPrescription(Integer patientId, Integer doctorId, Integer visitId) {
        Prescription prescription = new Prescription();
        prescription.setPatientId(patientId);
        prescription.setDoctorId(doctorId);
        prescription.setVisitId(visitId);
        prescription.setTotalCost((double) 0);
        prescriptionMapper.insert(prescription);
        return prescription;
    }

    public void updateTotalPrice(Integer prescriptionId) {
        //获取药单
        Prescription prescription = prescriptionMapper.selectById(prescriptionId);
        //获取该药单中含有的药品项目
        List<PrescriptionItem> prescriptionItems = prescriptionItemMapper.selectByPrescriptionId(prescriptionId);

        double totalCost = 0;

        //计算药品总费用
        for (PrescriptionItem item : prescriptionItems) {
            totalCost += item.getTotalPrice();
        }

        prescription.setTotalCost(totalCost);
        prescriptionMapper.updateById(prescription);
    }
}